package com.example.lj.uav.view;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.example.lj.uav.MyApplication;
import com.example.lj.uav.R;
import com.example.lj.uav.adapter.CameraAdapter;
import com.example.lj.uav.bean.CameraItem;
import com.example.lj.uav.bean.RouteLine;
import com.example.lj.uav.bean.RoutePoint;
import com.example.lj.uav.dao.MyDBDao;
import com.example.lj.uav.utils.DoubleUtils;
import com.example.lj.uav.view.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class RouteAreaSettingActivity extends BaseActivity implements View.OnClickListener ,Handler.Callback {
    private MapView mapView;
    private AMap aMap;
    private UiSettings uiSettings;
    private int markerIndex;
    private List<Polyline> polylineList;
    private String myLocationStr;
    private String name;
    private int start_flag;
    private int selectMarkerPosition;
    private boolean hasEdit;
    private MyDBDao dao;
    private RouteLine line;
    private final double longitudeMetre = 0.00001141;
    private final double latitudeMetre = 0.00000899;
    private List<Marker> markerList = new ArrayList<>();
    private List<RoutePoint> points = new ArrayList<>();//航点
    private List<String> fileNames;
    private boolean setComplete = false;
    private static final int CREATE = 0;
    private static final int UPDATE = 1;
    private boolean isRouteMenuVisible;
    private LinearLayout mLlButtonBar;
    private LinearLayout mLlButtonBar2;
    private ImageView mIvInfoToggle;
    private PopupWindow mPopupWindow;
    private RelativeLayout mRlMapType;
    private RelativeLayout mRlSave;
    private RelativeLayout mRlEdit;
    private RelativeLayout mRlCompass;
    private ImageView mIvCompass;
    private RelativeLayout mRlClear;
    private RelativeLayout mRlCurrentLocation;
    private ImageButton mIbFileName;
    private RelativeLayout mRlDeleteLast;

    //rightMenu
    private EditText mEtFileName;
    private RelativeLayout mRlInfoBtn;
    private FrameLayout mLlInfo;
    private Spinner mSpCamara;
    private ImageView mIvAltitudeReduce;
    private ImageView mIvAltitudePlus;
    private TextView mTvAltitude;
    private EditText mEtAltitude;
    private ImageView mIvSpeedReduce;
    private ImageView mIvSpeedPlus;
    private TextView mTvSpeed;
    private EditText mEtSpeed;
    private Spinner mSpFinishAction;
    private RelativeLayout mRlSeniorSetting;
    private TextView mTvLongitude;
    private TextView mTvLatitude;
    private ImageButton mIbLongitudeReduce;
    private ImageButton mIbLongitudePlus;
    private ImageButton mIbLatitudeReduce;
    private ImageButton mIbLatitudePlus;
    private ImageView mIvOverlapWReduce;//旁向重叠率
    private ImageView mIvOverlapWPlus;
    private TextView mTvOverlapW;
    private EditText mEtOverlapW;
    private ImageView mIvOverlapHReduce;//航向重叠率
    private ImageView mIvOverlapHPlus;
    private TextView mTvOverlapH;
    private EditText mEtOverlapH;
    private ImageView mIvDirectionReduce;//主航线角度
    private ImageView mIvDirectionPlus;
    private TextView mTvDirection;
    private EditText mEtDirection;
    private ImageView mIvMarginReduce;//边距
    private ImageView mIvMarginPlus;
    private TextView mTvMargin;
    private EditText mEtMargin;
    private View mCustomCamera;
    private View mCreateCamera;
    private RelativeLayout mRlCustomCamera;
    private ImageView mIvCustomCameraBack;
    private RecyclerView mRevCustomCameraList;
    private Button mBtnCreateCamera;
    private TextView mTvCameraName;
    private EditText mEtCameraName;
    private TextView mTvSensorW;
    private EditText mEtSensorW;
    private TextView mTvSensorH;
    private EditText mEtSensorH;
    private TextView mTvFocalLength;
    private EditText mEtFocalLength;
    private TextView mTvDelete;
    private TextView mTvComplete;
    private ScrollView mSvAreaSetting;
    private ImageView mIvCreateCameraBack;
    private ImageView mBackToInfo;

    //list_media data
    private String[] cameraNameList = {"Phantom 4","Phantom 4 Pro","Phantom 4 RTK","Phantom 4 Pro V2.0","Phantom 4 Advanced","自定义相机"};
    private List<CameraItem> cameraItems = new ArrayList<>();
    private ArrayAdapter<String> cameraAdapter;
    private CameraItem selectedCamera;
    private boolean isCameraSeleceted;
    private String[] motionIds = {"Hover","GoHome","AutoLand","GoFirstPoint"};//悬停，自动返航，原地降落，返回航线起始点
    private String[] motions = {"悬停","自动返航","原地降落","返回航线起始点"};
    private ArrayAdapter motionAdapter;
    private String motion;
    private List<CameraItem> customCameras = new ArrayList<>();
    private CameraItem currentCamera;
    private CameraAdapter customCameraAdapter;
    private Handler handler;
    private static final int CREATE_CUSTOM_CAMERA = 5;
    private static final int EXIST_CAMRERA_SELECTED = 6;
    private int customFlag;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_route_area_setting);
        initMap(savedInstanceState);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        mIbFileName.setOnClickListener(this);
        mRlClear.setOnClickListener(this);
        findViewById(R.id.iv_save).setOnClickListener(this);
        findViewById(R.id.iv_edit).setOnClickListener(this);
        mRlInfoBtn.setOnClickListener(this);
        mRlDeleteLast.setOnClickListener(this);
        mRlCurrentLocation.setOnClickListener(this);
        mRlCompass.setOnClickListener(this);
        mRlMapType.setOnClickListener(this);

        //设置menu点击事件
        mRlCustomCamera.setOnClickListener(this);
        mIvAltitudeReduce.setOnClickListener(this);
        mIvAltitudePlus.setOnClickListener(this);
        mTvAltitude.setOnClickListener(this);
        mIvSpeedReduce.setOnClickListener(this);
        mIvSpeedPlus.setOnClickListener(this);
        mTvSpeed.setOnClickListener(this);
        mIbLatitudePlus.setOnClickListener(this);
        mIbLatitudeReduce.setOnClickListener(this);
        mIbLongitudeReduce.setOnClickListener(this);
        mIbLongitudePlus.setOnClickListener(this);
        mIvOverlapWReduce.setOnClickListener(this);
        mIvOverlapWPlus.setOnClickListener(this);
        mIvOverlapHReduce.setOnClickListener(this);
        mIvOverlapHPlus.setOnClickListener(this);
        mTvOverlapH.setOnClickListener(this);
        mTvOverlapW.setOnClickListener(this);
        mIvDirectionReduce.setOnClickListener(this);
        mIvDirectionPlus.setOnClickListener(this);
        mTvDirection.setOnClickListener(this);
        mIvMarginReduce.setOnClickListener(this);
        mTvMargin.setOnClickListener(this);
        mIvMarginPlus.setOnClickListener(this);
        mIvCustomCameraBack.setOnClickListener(this);
        mBtnCreateCamera.setOnClickListener(this);
        mTvCameraName.setOnClickListener(this);
        mTvSensorW.setOnClickListener(this);
        mTvSensorH.setOnClickListener(this);
        mTvFocalLength.setOnClickListener(this);
        mTvDelete.setOnClickListener(this);
        mTvComplete.setOnClickListener(this);
        mIvCreateCameraBack.setOnClickListener(this);
        mRlSeniorSetting.setOnClickListener(this);
        mBackToInfo.setOnClickListener(this);

        aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                int position = markerList.indexOf(marker);
                setMarkerChecked(position);
                return true;
            }
        });
    }

    private void initMap(Bundle savedInstanceState){
        mapView = findViewById(R.id.map1);
        mapView.onCreate(savedInstanceState);
        aMap = mapView.getMap();
        final MyLocationStyle myLocationStyle = new MyLocationStyle();
//        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW);//跟随，1秒刷新
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE) ;//显示
//        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER) ;//连续定位、蓝点不会移动到地图中心点，定位点依照设备方向旋转，并且蓝点会跟随设备移动。
        myLocationStyle.showMyLocation(true);//显示标记
        aMap.setMyLocationStyle(myLocationStyle);//设置定位模式
        uiSettings = aMap.getUiSettings();//获取uisetting
        uiSettings.setScaleControlsEnabled(true);//显示比例尺
        CameraUpdate mCameraUpdate = CameraUpdateFactory.zoomTo(17);//放缩等级17
        aMap.animateCamera(mCameraUpdate);
//        setMapClickListEnable(true);
        aMap.setOnMarkerDragListener(new AMap.OnMarkerDragListener() {
            @Override
            public void onMarkerDragStart(Marker marker) {
                marker.getPosition();
                for (int i = 0; i < markerList.size(); i++) {
                    if (markerList.get(i).getId().equals(marker.getId())) {
                        markerIndex = i;
                        break;
                    }
                }
            }

            @Override
            public void onMarkerDrag(Marker marker) {
                upDatePointAndLine(markerIndex, Color.RED);

            }

            @Override
            public void onMarkerDragEnd(Marker marker) {
                Log.e("拖动结束", marker.getPosition().toString());
                points.get(markerIndex).setLongitude(DoubleUtils.doubleFormat(marker.getPosition().longitude,7));
                points.get(markerIndex).setLatitude(DoubleUtils.doubleFormat(marker.getPosition().latitude,7));
//                fragmentPointSetting.updatePointPosition(markerIndex);
            }
        });
        aMap.setMyLocationEnabled(true);//允许定位
        aMap.setOnMyLocationChangeListener(new AMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {

                String s = location.toString();
                String d[] = s.split("#");
                String province = d[2].substring(d[2].indexOf("=")+1,d[2].length());
                String city = d[3].substring(d[3].indexOf("=")+1,d[3].length());
                String district = d[4].substring(d[4].indexOf("=")+1,d[4].length());
                myLocationStr = province+" "+city+" "+district;
            }
        });
    }

    public void upDatePointAndLine(int position,int color) {
        if (markerList.size() == 0) {
            return;
        }
        if (position == (markerList.size() - 1)) {
            polylineList.get(position - 1).remove();
            polylineList.set(position - 1, aMap.addPolyline(new PolylineOptions().add(markerList.get(position - 1).getPosition(), markerList.get(position).getPosition()).color(color)));
            polylineList.get(polylineList.size()-1).remove();
            polylineList.set(polylineList.size()-1, aMap.addPolyline(new PolylineOptions().add(markerList.get(markerList.size()-1).getPosition(), markerList.get(0).getPosition()).color(color)));
        } else if (position == 0) {
            polylineList.get(position).remove();
            polylineList.set(position, aMap.addPolyline(new PolylineOptions().add(markerList.get(position).getPosition(), markerList.get(position + 1).getPosition()).color(color)));
            polylineList.get(polylineList.size()-1).remove();
            polylineList.set(polylineList.size()-1, aMap.addPolyline(new PolylineOptions().add(markerList.get(markerList.size()-1).getPosition(), markerList.get(0).getPosition()).color(color)));
        } else {
            polylineList.get(position).remove();
            polylineList.get(position - 1).remove();
            polylineList.set(position, aMap.addPolyline(new PolylineOptions().add(markerList.get(position).getPosition(), markerList.get(position + 1).getPosition()).color(color)));
            polylineList.set(position - 1, aMap.addPolyline(new PolylineOptions().add(markerList.get(position - 1).getPosition(), markerList.get(position).getPosition()).color(color)));
        }
    }

    private void initData() {
        handler = new Handler(this);
        dao = MyApplication.dao;
        markerList = new ArrayList<>();
        polylineList = new ArrayList<>();
        Intent intent = getIntent();
        line = intent.getParcelableExtra("line");
        fileNames = (List<String>) intent.getSerializableExtra("names");
        name = intent.getStringExtra("name");
        start_flag = intent.getIntExtra("start_flag",-1);
        mEtFileName.setText(name);
        mEtFileName.setEnabled(false);//设置成false避免关屏开屏之后弹出输入界面
        if (line!=null){
            getPoints(line.getFileName());
        }else{
            line = new RouteLine();
            line.setSpeed(5.0f);
            line.setAltitude(100);
            line.setHeadingMode("Auto");
            line.setGimbalPitchMode("UsePointSetting");
            line.setActionOnFinish("GoFirstPoint");
            line.setCameraType("Phantom 4");
            line.setActionOnFinish("GoHome");
            line.setOverlapW("70");
            line.setOverlapH("80");
            line.setDirection("0");
            line.setMargin("25");
            line.setFileName(name);
        }
//        angle = line.getHeadingMode();
//        control = line.getGimbalPitchMode();
        motion = line.getActionOnFinish();
//        speed = line.getSpeed();
//        altitude = line.getAltitude();
        if (points.size()>0){
            LatLng latLng = new LatLng(points.get(0).getLatitude(),points.get(0).getLongitude());
            cameraMoveTo(latLng);
        }
        //设置相机数据
        cameraItems.add(new CameraItem("Phantom 4","3.6","6.2","4.65"));
        cameraItems.add(new CameraItem("Phantom 4 Pro","3.6","6.2","4.65"));
        cameraItems.add(new CameraItem("Phantom 4 RTK","3.6","6.2","4.65"));
        cameraItems.add(new CameraItem("Phantom 4 Pro V2.0","3.6","6.2","4.65"));
        cameraItems.add(new CameraItem("Phantom 4 Advanced","3.6","6.2","4.65"));

        cameraAdapter = new ArrayAdapter(this,R.layout.textview_spinner_drop_down,cameraNameList);
        cameraAdapter.setDropDownViewResource(R.layout.textview_spinner_drop_down);
        mSpCamara.setAdapter(cameraAdapter);
        mSpCamara.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (i<cameraNameList.length-1){
                    selectedCamera = cameraItems.get(i);
                    isCameraSeleceted = true;
                }else{
                    mRlCustomCamera.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        getCustomCameraList();
        showCameraList();
    }

    private void showCameraList() {
        motionAdapter = new ArrayAdapter(this,R.layout.textview_spinner_drop_down,motions);
        motionAdapter.setDropDownViewResource(R.layout.textview_spinner_drop_down);
        mSpFinishAction.setAdapter(motionAdapter);
        int motionPosition = 0;
        for (int i=0;i<motionIds.length;i++){
            if (motion.equals(motionIds[i])){
                motionPosition = i;
            }
        }
        mSpFinishAction.setSelection(motionPosition);
        mSpFinishAction.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                line.setActionOnFinish(motionIds[i]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    public void getCustomCameraList(){
        customCameras.clear();
        customCameras.addAll(dao.queryCamera());
        if (customCameras.size()>0){
            if (customCameraAdapter==null){
                customCameraAdapter = new CameraAdapter(this,customCameras,handler);
                mRevCustomCameraList.setAdapter(customCameraAdapter);
            }
            customCameraAdapter.notifyDataSetChanged();
        }
    }

    private void initView() {
        mRlClear = findViewById(R.id.ll_clear);
        mRlSave = findViewById(R.id.rl_save_btn);
        mRlEdit = findViewById(R.id.rl_edit_btn);
        mEtFileName = findViewById(R.id.et_file_name);
        mLlButtonBar = findViewById(R.id.ll_map_button_bar);
        mLlButtonBar2 = findViewById(R.id.ll_map_button_bar2);
        mIvInfoToggle = findViewById(R.id.iv_hide_menu);
        mRlInfoBtn = findViewById(R.id.rl_route_info_menu_btn);
        mLlInfo = findViewById(R.id.fl_route_info_menu);
        mRlDeleteLast = findViewById(R.id.rl_delete_last);
        mRlCurrentLocation = findViewById(R.id.rl_self_location);
        mRlCompass = findViewById(R.id.rl_compass);
        mIvCompass = findViewById(R.id.iv_compass);
        mRlMapType = findViewById(R.id.rl_map_type);
        mIbFileName = findViewById(R.id.ib_file_name_editable);
        mSpCamara = findViewById(R.id.sp_area_camera);
        mIvAltitudeReduce = findViewById(R.id.iv_line_altitude_reduce);
        mIvAltitudePlus = findViewById(R.id.iv_line_altitude_plus);
        mTvAltitude = findViewById(R.id.tv_line_altitude);
        mEtAltitude = findViewById(R.id.et_line_altitude);
        mIvSpeedReduce = findViewById(R.id.iv_line_speed_reduce);
        mIvSpeedPlus = findViewById(R.id.iv_line_speed_plus);
        mTvSpeed = findViewById(R.id.tv_line_speed);
        mEtSpeed = findViewById(R.id.et_line_speed);
        mSpFinishAction = findViewById(R.id.sp_line_motion);
        mRlSeniorSetting = findViewById(R.id.rl_area_senior_setting);
        mTvLongitude = findViewById(R.id.tv_point_longitude);
        mTvLatitude = findViewById(R.id.tv_point_latitude);
        mIbLongitudeReduce = findViewById(R.id.iv_longitude_reduce);
        mIbLongitudePlus = findViewById(R.id.iv_longitude_plus);
        mIbLatitudeReduce = findViewById(R.id.iv_latitude_reduce);
        mIbLatitudePlus = findViewById(R.id.iv_latitude_plus);
        mIvOverlapWReduce = findViewById(R.id.iv_side_overlapped_reduce);
        mIvOverlapWPlus = findViewById(R.id.iv_side_overlapped_plus);
        mTvOverlapW = findViewById(R.id.tv_side_overlapped);
        mEtOverlapW = findViewById(R.id.et_side_overlapped);
        mIvOverlapHReduce = findViewById(R.id.iv_forward_overlapped_reduce);
        mIvOverlapHPlus= findViewById(R.id.iv_forward_overlapped_plus);
        mTvOverlapH= findViewById(R.id.tv_forward_overlapped);
        mEtOverlapH= findViewById(R.id.et_forward_overlapped);
        mIvDirectionReduce= findViewById(R.id.iv_forward_angle_reduce);
        mIvDirectionPlus= findViewById(R.id.iv_forward_angle_plus);
        mTvDirection= findViewById(R.id.tv_forward_angle);
        mEtDirection= findViewById(R.id.et_forward_angle);
        mIvMarginReduce= findViewById(R.id.iv_margin_reduce);
        mIvMarginPlus= findViewById(R.id.iv_margin_plus);
        mTvMargin= findViewById(R.id.tv_margin);
        mEtMargin= findViewById(R.id.et_margin);
        mCustomCamera= findViewById(R.id.ll_custom_camera);
        mCreateCamera= findViewById(R.id.ll_create_custom_camera);
        mRlCustomCamera= findViewById(R.id.rl_area_custom_camera_setting);
        mIvCustomCameraBack= findViewById(R.id.iv_custom_camera_back);
        mBtnCreateCamera= findViewById(R.id.btn_custom_camera_create);
        mTvCameraName= findViewById(R.id.tv_create_camera_name);
        mEtCameraName= findViewById(R.id.et_create_camera_name);
        mTvSensorW= findViewById(R.id.tv_create_camera_sensor_w);
        mEtSensorW= findViewById(R.id.et_create_camera_sensor_w);
        mTvSensorH= findViewById(R.id.tv_create_camera_sensor_h);
        mEtSensorH= findViewById(R.id.et_create_camera_sensor_h);
        mTvFocalLength= findViewById(R.id.tv_create_camera_focal_length);
        mEtFocalLength= findViewById(R.id.et_create_camera_focal_length);
        mTvDelete= findViewById(R.id.tv_delete);
        mTvComplete= findViewById(R.id.tv_complete);
        mSvAreaSetting = findViewById(R.id.sv_area_setting);
        mIvCreateCameraBack = findViewById(R.id.iv_create_custom_camera_back);
        mBackToInfo = findViewById(R.id.iv_back_to_area_info);

        mRlCustomCamera.setVisibility(View.GONE);

        mRevCustomCameraList = findViewById(R.id.rev_custom_camera_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRevCustomCameraList.setLayoutManager(linearLayoutManager);

        mEtFileName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (EditorInfo.IME_ACTION_DONE==actionId||EditorInfo.IME_ACTION_NEXT==actionId||
                        EditorInfo.IME_ACTION_GO==actionId||EditorInfo.IME_ACTION_SEND==actionId){
                    for (int i=0;i<customCameras.size();i++){
                        if (name.equals(customCameras.get(i).getName())){
                            showToast("名字已存在");
                            return false;
                        }
                    }
                    line.setFileName(mEtFileName.getText().toString());
                }
                return false;
            }
        });
        mEtAltitude.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (EditorInfo.IME_ACTION_DONE==actionId||EditorInfo.IME_ACTION_NEXT==actionId||
                        EditorInfo.IME_ACTION_GO==actionId||EditorInfo.IME_ACTION_SEND==actionId){
                    double altitude = Double.parseDouble(mEtAltitude.getText().toString().trim());
                    if (altitude>=500){
                        mTvAltitude.setText("500m");
                    }else if (altitude<=25){
                        mTvAltitude.setText("25m");
                    }else{
                        mTvAltitude.setText(altitude+"m");
                    }
//                    line.
                    mTvAltitude.setVisibility(View.VISIBLE);
                    mEtAltitude.setVisibility(View.GONE);
                }
                return false;
            }
        });

        mEtSpeed.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (EditorInfo.IME_ACTION_DONE==actionId||EditorInfo.IME_ACTION_NEXT==actionId||
                        EditorInfo.IME_ACTION_GO==actionId||EditorInfo.IME_ACTION_SEND==actionId){
                    double speed = Double.parseDouble(mEtSpeed.getText().toString().trim());
                    if (speed>=2.5){
                        mTvSpeed.setText("2.5m/s");
                    }else if (speed<=1){
                        mTvSpeed.setText("1m/s");
                    }else{
                        mTvSpeed.setText(speed+"m/s");
                    }
                    mTvSpeed.setVisibility(View.VISIBLE);
                    mEtSpeed.setVisibility(View.GONE);
                }
                return false;
            }
        });

        mEtOverlapW.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (EditorInfo.IME_ACTION_DONE==actionId||EditorInfo.IME_ACTION_NEXT==actionId||
                        EditorInfo.IME_ACTION_GO==actionId||EditorInfo.IME_ACTION_SEND==actionId){
                    double content = Double.parseDouble(mEtOverlapW.getText().toString().trim());
                    if (content>=90){
                        mTvSpeed.setText("90%");
                    }else if (content<=10){
                        mTvSpeed.setText("10%");
                    }else{
                        mTvOverlapW.setText(content+"%");
                    }
                    mTvOverlapW.setVisibility(View.VISIBLE);
                    mEtOverlapW.setVisibility(View.GONE);
                }
                return false;
            }
        });

        mEtOverlapH.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (EditorInfo.IME_ACTION_DONE==actionId||EditorInfo.IME_ACTION_NEXT==actionId||
                        EditorInfo.IME_ACTION_GO==actionId||EditorInfo.IME_ACTION_SEND==actionId){
                    double content = Double.parseDouble(mEtOverlapH.getText().toString().trim());
                    if (content>=90){
                        mTvSpeed.setText("90%");
                    }else if (content<=10){
                        mTvSpeed.setText("10%");
                    }else{
                        mTvOverlapH.setText(content+"%");
                    }
                    mTvOverlapH.setVisibility(View.VISIBLE);
                    mEtOverlapH.setVisibility(View.GONE);
                }
                return false;
            }
        });

        mEtDirection.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (EditorInfo.IME_ACTION_DONE==actionId||EditorInfo.IME_ACTION_NEXT==actionId||
                        EditorInfo.IME_ACTION_GO==actionId||EditorInfo.IME_ACTION_SEND==actionId){
                    double content = Double.parseDouble(mEtDirection.getText().toString().trim());
                    if (content>=359){
                        mTvDirection.setText("359°");
                    }else if (content<=0){
                        mTvDirection.setText("0°");
                    }else{
                        mTvDirection.setText(content+"°");
                    }
                    mTvDirection.setVisibility(View.VISIBLE);
                    mEtDirection.setVisibility(View.GONE);
                }
                return false;
            }
        });

        mEtMargin.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (EditorInfo.IME_ACTION_DONE==actionId||EditorInfo.IME_ACTION_NEXT==actionId||
                        EditorInfo.IME_ACTION_GO==actionId||EditorInfo.IME_ACTION_SEND==actionId){
                    double content = Double.parseDouble(mEtMargin.getText().toString().trim());
                    if (content>=7.5){
                        mTvMargin.setText("7.5m");
                    }else if (content<=2.5){
                        mTvMargin.setText("2.5m");
                    }else{
                        mTvMargin.setText(content+"m");
                    }
                    mTvMargin.setVisibility(View.VISIBLE);
                    mEtMargin.setVisibility(View.GONE);
                }

                return false;
            }
        });

        mEtSensorW.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (EditorInfo.IME_ACTION_DONE==actionId||EditorInfo.IME_ACTION_NEXT==actionId||
                        EditorInfo.IME_ACTION_GO==actionId||EditorInfo.IME_ACTION_SEND==actionId){
                    double content = Double.parseDouble(mEtSensorW.getText().toString().trim());
                    if (content>=200){
                        mTvSensorW.setText("200mm");
                    }else if (content<=1){
                        mTvSensorW.setText("1mm");
                    }else{
                        mTvSensorW.setText(content+"mm");
                    }
                    mTvSensorW.setVisibility(View.VISIBLE);
                    mEtSensorW.setVisibility(View.GONE);
                }
                return false;
            }
        });

        mEtSensorH.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (EditorInfo.IME_ACTION_DONE==actionId||EditorInfo.IME_ACTION_NEXT==actionId||
                        EditorInfo.IME_ACTION_GO==actionId||EditorInfo.IME_ACTION_SEND==actionId){
                    double content = Double.parseDouble(mEtSensorH.getText().toString().trim());
                    if (content>=200){
                        mEtSensorH.setText("200mm");
                    }else if (content<=1){
                        mEtSensorH.setText("1mm");
                    }else{
                        mEtSensorH.setText(content+"mm");
                    }
                    mTvSensorH.setVisibility(View.VISIBLE);
                    mEtSensorH.setVisibility(View.GONE);
                }
                return false;
            }
        });

        mEtFocalLength.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (EditorInfo.IME_ACTION_DONE==actionId||EditorInfo.IME_ACTION_NEXT==actionId||
                        EditorInfo.IME_ACTION_GO==actionId||EditorInfo.IME_ACTION_SEND==actionId){
                    double content = Double.parseDouble(mEtFocalLength.getText().toString().trim());
                    if (content>=200){
                        mTvFocalLength.setText("200mm");
                    }else if (content<=1){
                        mTvFocalLength.setText("1mm");
                    }else{
                        mTvFocalLength.setText(content+"mm");
                    }
                    mTvFocalLength.setVisibility(View.VISIBLE);
                    mEtFocalLength.setVisibility(View.GONE);
                }
                return false;
            }
        });


    }

    private void cameraMoveTo(LatLng latLng) {
        double latitude = latLng.latitude;
        double longitude = latLng.longitude;
        aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(latitude,longitude)));
    }

    private void getPoints(String fileName) {
        points.clear();
        points.addAll(dao.queryPoints(fileName));
        aMap.clear();
        markerList.clear();
        polylineList.clear();
        drawMarkerAndPolyline(points,Color.BLUE);
    }

    private void drawMarkerAndPolyline(List<RoutePoint> points,int color) {
        for (int i=0;i<points.size()+1;i++){
            if (i!=points.size()){
                double longitude = points.get(i).getLongitude();
                double latitude = points.get(i).getLatitude();
                LatLng latLng = new LatLng(latitude,longitude);
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(latLng);
                final BitmapDescriptor bd = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.ic_waypoint_normal));
                markerOptions.icon(bd);
                markerOptions.anchor(0.5f,0.5f);
                Marker marker = aMap.addMarker(markerOptions);
                marker.setDraggable(true);
                markerList.add(marker);
                int length = markerList.size();
                if (length > 1) {//不包括定位点
                    polylineList.add(aMap.addPolyline(new PolylineOptions().add(markerList.get(length - 2).getPosition(), markerList.get(length - 1).getPosition()).color(color)));
                }
            }else{
                polylineList.add(aMap.addPolyline(new PolylineOptions().add(markerList.get(markerList.size()-1).getPosition(), markerList.get(0).getPosition()).color(color)));
            }
        }
    }

    private void setMapClickListEnable(boolean enable){
        if (enable){
            aMap.setOnMapClickListener(new AMap.OnMapClickListener() {
                @Override
                public void onMapClick(LatLng latLng) {
                    if (aMap.getCameraPosition().zoom<17){
                        CameraUpdate mCameraUpdate = CameraUpdateFactory.zoomTo(17);//放缩等级17
                        aMap.animateCamera(mCameraUpdate);
                    }
                    RoutePoint point = new RoutePoint(latLng.longitude,latLng.latitude);
                    RoutePoint point1 = new RoutePoint(latLng.longitude-longitudeMetre*50,latLng.latitude+latitudeMetre*50);
                    RoutePoint point2 = new RoutePoint(latLng.longitude+longitudeMetre*50,latLng.latitude+latitudeMetre*50);
                    RoutePoint point3 = new RoutePoint(latLng.longitude+longitudeMetre*50,latLng.latitude-latitudeMetre*50);
                    RoutePoint point4 = new RoutePoint(latLng.longitude-longitudeMetre*50,latLng.latitude-latitudeMetre*50);
                    points.add(point);
                    points.add(point1);
                    points.add(point2);
                    points.add(point3);
                    points.add(point4);
                    drawMarkerAndPolyline(points,Color.BLUE);
                    hasEdit = true;
                    setMapClickListEnable(false);
                }
            });
        }else{
            aMap.setOnMapClickListener(new AMap.OnMapClickListener() {
                @Override
                public void onMapClick(LatLng latLng) {
                }
            });
        }
    }

    public void setMarkerChecked(int position){
        for (int i=0;i<markerList.size();i++){
            if (position==i){
                markerList.get(i).setIcon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.ic_waypoint_selected)));
            }else{
                markerList.get(i).setIcon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.ic_waypoint_normal)));
            }
        }
        selectMarkerPosition = position;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (setComplete){
            return;
        }
        if (start_flag == CREATE){
            mRlEdit.setVisibility(View.GONE);
            mRlSave.setVisibility(View.VISIBLE);
            mIvInfoToggle.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.arrow_right));
            isRouteMenuVisible = true;
            setMapClickListEnable(true);
        }else{
            mRlEdit.setVisibility(View.VISIBLE);
            mRlSave.setVisibility(View.GONE);
            mIvInfoToggle.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.arrow_left));
            addAnimationHide(mLlInfo,50);
            addAnimationHide(mRlInfoBtn,50);
            addAnimationHide(mLlButtonBar,50);
            mRlInfoBtn.setVisibility(View.INVISIBLE);
            mLlButtonBar2.setVisibility(View.GONE);
            isRouteMenuVisible = false;
            setMapClickListEnable(false);
        }
        CameraUpdate mCameraUpdate = CameraUpdateFactory.zoomTo(17);//放缩等级17
        aMap.animateCamera(mCameraUpdate);
//        setComplete = true;
    }


    private void addAnimationHide(View view,int duration){
        float currentX = mLlInfo.getTranslationX();
        float menuX = mLlInfo.getWidth();
        ObjectAnimator animatorHide = ObjectAnimator.ofFloat(view,"translationX",currentX,menuX);
        animatorHide.setDuration(duration);
        animatorHide.start();
        isRouteMenuVisible = false;
    }

    private void addAnimationShow(View view,int duration){
        float menuX = mLlInfo.getWidth();
        ObjectAnimator animatorHide = ObjectAnimator.ofFloat(view,"translationX",menuX,0);
        animatorHide.setDuration(duration);
        animatorHide.start();
        isRouteMenuVisible = true;
    }

    @Override
    public void onClick(View view) {
        RoutePoint point = points.get(selectMarkerPosition);
        switch (view.getId()){
            case R.id.ll_clear:
                for (int i=0;i<markerList.size();i++){
                    markerList.get(i).destroy();
                }
                for (int i=0;i<polylineList.size();i++){
                    polylineList.get(i).remove();
                }
                polylineList.clear();
                markerList.clear();
                points.clear();
                setMapClickListEnable(true);
                break;
            case R.id.iv_save:
//                saveRoute();
                break;
            case R.id.iv_edit:
                hasEdit = true;
                mRlEdit.setVisibility(View.GONE);
                mRlSave.setVisibility(View.VISIBLE);
                mLlButtonBar2.setVisibility(View.VISIBLE);
                mRlInfoBtn.setVisibility(View.VISIBLE);
                setMapClickListEnable(true);
                addAnimationShow(mLlInfo,500);
                addAnimationShow(mRlInfoBtn,500);
                addAnimationShow(mLlButtonBar,500);
                mIvInfoToggle.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.arrow_right));
                break;
            case R.id.rl_route_info_menu_btn:
                if (isRouteMenuVisible){
                    addAnimationHide(mLlInfo,500);
                    addAnimationHide(mRlInfoBtn,500);
                    addAnimationHide(mLlButtonBar,500);
                    mIvInfoToggle.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.arrow_left));
                    isRouteMenuVisible = false;
                }else{
                    addAnimationShow(mLlInfo,500);
                    addAnimationShow(mRlInfoBtn,500);
                    addAnimationShow(mLlButtonBar,500);
                    mIvInfoToggle.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.arrow_right));
                    isRouteMenuVisible = true;
                }
                break;
            case R.id.rl_delete_last:
                int index = markerList.size()-1;
                if (index>=0){
                    markerList.get(index).destroy();
                    markerList.remove(index);
                }
                int lineIndex = polylineList.size()-1;
                if (lineIndex>=0){
                    polylineList.get(lineIndex).remove();
                    polylineList.remove(lineIndex);
                }
                int pointIndex = points.size()-1;
                if (pointIndex>=0){
                    points.remove(pointIndex);

                }
                if (pointIndex == selectMarkerPosition){
                    setMarkerChecked(pointIndex-1);
                }
                break;
            case R.id.rl_self_location:
                Location location = aMap.getMyLocation();
                LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
                cameraMoveTo(latLng);
                break;
            case R.id.rl_compass:
                if (mIvCompass.isEnabled()){
                    mIvCompass.setEnabled(false);
                }else{
                    mIvCompass.setEnabled(true);
                }
                break;
            case R.id.rl_map_type:
                showPopupWindow();
                break;
            case R.id.ll_standard_map:
                aMap.setMapType(com.amap.api.maps2d.AMap.MAP_TYPE_NORMAL);
                break;
            case R.id.ll_satellite_map:
                aMap.setMapType(com.amap.api.maps2d.AMap.MAP_TYPE_SATELLITE);
                break;
            case R.id.ib_file_name_editable:
                mEtFileName.setEnabled(true);
                break;
            case R.id.rl_area_custom_camera_setting:
                if (customCameras.size()>0){
                    mCustomCamera.setVisibility(View.VISIBLE);
                    mCreateCamera.setVisibility(View.GONE);
                    mSvAreaSetting.setVisibility(View.GONE);
                }else{
                    customFlag = CREATE_CUSTOM_CAMERA;
                    mCreateCamera.setVisibility(View.VISIBLE);
                    mCustomCamera.setVisibility(View.GONE);
                    mSvAreaSetting.setVisibility(View.GONE);
                    currentCamera = new CameraItem();
                    currentCamera.setName("新建相机");
                    currentCamera.setSensorW("10.0");
                    currentCamera.setSensorH("10.0");
                    currentCamera.setFocal_length("10.0");
                    mTvCameraName.setText(currentCamera.getName());
                    mTvSensorW.setText(currentCamera.getSensorW()+"mm");
                    mTvSensorH.setText(currentCamera.getSensorH()+"mm");
                    mTvFocalLength.setText(currentCamera.getFocal_length()+"mm");
                }
                break;
            case R.id.iv_line_altitude_reduce:
                double altitudeR = line.getAltitude();
                if (altitudeR<=0){
                    return;
                }
                altitudeR = altitudeR-1;
                line.setAltitude(altitudeR);
                mTvAltitude.setText(altitudeR+"m");
                mEtAltitude.setVisibility(View.GONE);
                mTvAltitude.setVisibility(View.VISIBLE);
                break;
            case R.id.iv_line_altitude_plus:
                double altitudeP = line.getAltitude();
                if (altitudeP>=500){
                    return;
                }
                altitudeP = altitudeP+1;
                line.setAltitude(altitudeP);
                mTvAltitude.setText(altitudeP+"m");
                mEtAltitude.setVisibility(View.GONE);
                mTvAltitude.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_line_altitude:
                double altitudeE = line.getAltitude();
                mEtAltitude.setText(altitudeE+"");
                mTvAltitude.setVisibility(View.GONE);
                mEtAltitude.setVisibility(View.VISIBLE);
                break;
            case R.id.iv_line_speed_reduce:
                double speedR = line.getSpeed();
                if (speedR<=0){
                    return;
                }
                speedR = speedR-0.1;
                line.setSpeed(speedR);
                mTvSpeed.setVisibility(View.VISIBLE);
                mEtSpeed.setVisibility(View.GONE);
                mTvSpeed.setText(speedR+"m/s");
                break;
            case R.id.iv_line_speed_plus:
                double speedP = line.getSpeed();
                if (speedP>=15){
                    return;
                }
                speedP = speedP+0.1;
                line.setSpeed(speedP);
                mTvSpeed.setVisibility(View.VISIBLE);
                mEtSpeed.setVisibility(View.GONE);
                mTvSpeed.setText(speedP+"m/s");
                break;
            case R.id.tv_line_speed:
                double speedE = line.getSpeed();
                mEtSpeed.setText(speedE+"");
                mTvSpeed.setVisibility(View.GONE);
                mEtSpeed.setVisibility(View.VISIBLE);
                break;
            case R.id.iv_longitude_plus:
                double longitude = point.getLongitude();
                longitude = DoubleUtils.doubleFormat(longitude + 0.000005,7);
                point.setLongitude(longitude);
                mTvLongitude.setText(longitude+"");
                upDatePointAndLine(selectMarkerPosition,Color.YELLOW);
                break;
            case R.id.iv_longitude_reduce:
                double longitude1 = point.getLongitude();
                longitude1 = DoubleUtils.doubleFormat(longitude1 - 0.000005,7);
                point.setLongitude(longitude1);
                mTvLongitude.setText(longitude1+"");
                upDatePointAndLine(selectMarkerPosition,Color.YELLOW);
                break;
            case R.id.iv_latitude_plus:
                double lat = point.getLatitude();
                lat = DoubleUtils.doubleFormat(lat + 0.000005,7);
                point.setLatitude(lat);
                mTvLatitude.setText(lat+"");
                upDatePointAndLine(selectMarkerPosition,Color.YELLOW);
                break;
            case R.id.iv_latitude_reduce:
                double lat1 = point.getLatitude();
                lat1 = DoubleUtils.doubleFormat(lat1 - 0.000005,7);
                point.setLatitude(lat1);
                mTvLatitude.setText(lat1+"");
                upDatePointAndLine(selectMarkerPosition,Color.YELLOW);
                break;
            case R.id.rl_area_senior_setting:
                mRlSeniorSetting.setVisibility(View.VISIBLE);
                mCreateCamera.setVisibility(View.GONE);
                mCustomCamera.setVisibility(View.GONE);
                mSvAreaSetting.setVisibility(View.GONE);
                break;
            case R.id.iv_back_to_area_info:
                mRlSeniorSetting.setVisibility(View.GONE);
                mSvAreaSetting.setVisibility(View.VISIBLE);
                break;
            case R.id.iv_side_overlapped_reduce:
                double overlappedWR = Double.parseDouble(line.getOverlapW());
                overlappedWR = overlappedWR-0.1;
                mTvOverlapW.setText(overlappedWR+"%");
                line.setOverlapW(overlappedWR+"");
                mTvOverlapW.setVisibility(View.VISIBLE);
                mEtOverlapW.setVisibility(View.GONE);
                break;
            case R.id.iv_side_overlapped_plus:
                double overlappedWP = Double.parseDouble(line.getOverlapW());
                overlappedWP = overlappedWP+0.1;
                mTvOverlapW.setText(overlappedWP+"%");
                line.setOverlapW(overlappedWP+"");
                mTvOverlapW.setVisibility(View.VISIBLE);
                mEtOverlapW.setVisibility(View.GONE);
                break;
            case R.id.tv_side_overlapped:
                double overlappedWE = Double.parseDouble(line.getOverlapW());
                mEtOverlapW.setText(overlappedWE+"%");
                mEtOverlapW.setVisibility(View.VISIBLE);
                mTvOverlapW.setVisibility(View.GONE);
                break;
            case R.id.iv_forward_overlapped_reduce:
                double overlappedHR = Double.parseDouble(line.getOverlapH());
                overlappedHR = overlappedHR-0.1;
                mTvOverlapH.setText(overlappedHR+"%");
                line.setOverlapH(overlappedHR+"");
                mTvOverlapH.setVisibility(View.VISIBLE);
                mEtOverlapH.setVisibility(View.GONE);
                break;
            case R.id.iv_forward_overlapped_plus:
                double overlappedHP = Double.parseDouble(line.getOverlapH());
                overlappedHP = overlappedHP+0.1;
                mTvOverlapH.setText(overlappedHP+"%");
                line.setOverlapH(overlappedHP+"");
                mTvOverlapH.setVisibility(View.VISIBLE);
                mEtOverlapH.setVisibility(View.GONE);
                break;
            case R.id.tv_forward_overlapped:
                double overlappedHE = Double.parseDouble(line.getOverlapH());
                mEtOverlapH.setText(overlappedHE+"%");
                mEtOverlapH.setVisibility(View.VISIBLE);
                mTvOverlapH.setVisibility(View.GONE);
                break;
            case R.id.iv_forward_angle_reduce:
                double angleR = Double.parseDouble(line.getDirection());
                angleR = angleR-0.1;
                mTvDirection.setText(angleR+"%");
                line.setOverlapH(angleR+"");
                mTvDirection.setVisibility(View.VISIBLE);
                mEtDirection.setVisibility(View.GONE);
                break;
            case R.id.iv_forward_angle_plus:
                double angleP = Double.parseDouble(line.getDirection());
                angleP = angleP+0.1;
                mTvDirection.setText(angleP+"°");
                line.setOverlapH(angleP+"");
                mTvDirection.setVisibility(View.VISIBLE);
                mEtDirection.setVisibility(View.GONE);
                break;
            case R.id.tv_forward_angle:
                double angleE = Double.parseDouble(line.getOverlapH());
                mEtDirection.setText(angleE+"°");
                mEtDirection.setVisibility(View.VISIBLE);
                mTvDirection.setVisibility(View.GONE);
                break;
            case R.id.iv_margin_reduce:
                double marginR = Double.parseDouble(line.getDirection());
                marginR = marginR-0.1;
                mTvMargin.setText(marginR+"m");
                line.setOverlapH(marginR+"");
                mTvMargin.setVisibility(View.VISIBLE);
                mEtMargin.setVisibility(View.GONE);
                break;
            case R.id.iv_margin_plus:
                double marginH = Double.parseDouble(line.getDirection());
                marginH = marginH+0.1;
                mTvMargin.setText(marginH+"m");
                line.setOverlapH(marginH+"");
                mTvMargin.setVisibility(View.VISIBLE);
                mEtMargin.setVisibility(View.GONE);
                break;
            case R.id.tv_margin:
                double marginE = Double.parseDouble(line.getOverlapH());
                mEtDirection.setText(marginE+"°");
                mEtDirection.setVisibility(View.VISIBLE);
                mTvDirection.setVisibility(View.GONE);
                break;
            case R.id.iv_custom_camera_back:
                mSvAreaSetting.setVisibility(View.VISIBLE);
                mCreateCamera.setVisibility(View.GONE);
                mCustomCamera.setVisibility(View.GONE);
                break;
            case R.id.iv_create_custom_camera_back:
                mSvAreaSetting.setVisibility(View.GONE);
                mCreateCamera.setVisibility(View.GONE);
                mCustomCamera.setVisibility(View.VISIBLE);
                getCustomCameraList();
                break;
            case R.id.btn_custom_camera_create:
                customFlag = CREATE_CUSTOM_CAMERA;
                mCreateCamera.setVisibility(View.VISIBLE);
                mCustomCamera.setVisibility(View.GONE);
                mSvAreaSetting.setVisibility(View.GONE);
                int position = 0;
                for (int i=0;i<customCameras.size();i++){
                    if (customCameras.get(i).getName().contains("新建相机"));
                    position++;
                }
                currentCamera = new CameraItem();
                currentCamera.setName("新建相机"+position);
                currentCamera.setSensorW("10.0");
                currentCamera.setSensorH("10.0");
                currentCamera.setFocal_length("10.0");
                mTvCameraName.setText(currentCamera.getName());
                mTvSensorW.setText(currentCamera.getSensorW()+"mm");
                mTvSensorH.setText(currentCamera.getSensorH()+"mm");
                mTvFocalLength.setText(currentCamera.getFocal_length()+"mm");
                break;
            case R.id.tv_create_camera_name:
                String cameraName = line.getCameraType();
                mTvCameraName.setVisibility(View.GONE);
                mEtCameraName.setVisibility(View.VISIBLE);
                mEtCameraName.setText(cameraName);
                break;
            case R.id.tv_create_camera_sensor_w:
                String sensorW = line.getSensorW();
                mTvSensorW.setVisibility(View.GONE);
                mEtSensorW.setVisibility(View.VISIBLE);
                mEtSensorW.setText(sensorW);
                break;
            case R.id.tv_create_camera_sensor_h:
                String sensorH = line.getSensorH();
                mTvSensorH.setVisibility(View.GONE);
                mEtSensorH.setVisibility(View.VISIBLE);
                mEtSensorH.setText(sensorH);
                break;
            case R.id.tv_create_camera_focal_length:
                String focal = line.getFocalLength();
                mTvFocalLength.setVisibility(View.GONE);
                mEtFocalLength.setVisibility(View.VISIBLE);
                mEtFocalLength.setText(focal);
                break;
            case R.id.tv_delete:
                if (customFlag==CREATE_CUSTOM_CAMERA){
                    mCreateCamera.setVisibility(View.GONE);
                    mCustomCamera.setVisibility(View.VISIBLE);
                    return;
                }
                dao.deleteCamera(currentCamera.getName());
                mCreateCamera.setVisibility(View.GONE);
                mCustomCamera.setVisibility(View.VISIBLE);
                getCustomCameraList();
                break;
            case R.id.tv_complete:
                if (customFlag==CREATE_CUSTOM_CAMERA){
                    String name = mTvCameraName.getText().toString().trim();
                    for (int i=0;i<customCameras.size();i++){
                        if (name.equals(customCameras.get(i).getName())){
                            showToast("名字已存在");
                            break;
                        }
                    }
                    CameraItem item = new CameraItem();
                    item.setName(name);
                    item.setSensorW(mTvSensorW.getText().toString().trim());
                    item.setSensorH(mTvSensorH.getText().toString().trim());
                    item.setFocal_length(mTvFocalLength.getText().toString().trim());
                    dao.addCamera(item);
                    mCreateCamera.setVisibility(View.GONE);
                    mCustomCamera.setVisibility(View.VISIBLE);
                    getCustomCameraList();
                }else{
                    for (int i=0;i<customCameras.size();i++){
                        if (name.equals(customCameras.get(i).getName())){
                            if (name.equals(currentCamera.getName())){
                                continue;
                            }
                            showToast("名字已存在");
                            break;
                        }
                    }
                    CameraItem item = new CameraItem();
                    item.setName(name);
                    item.setSensorW(mTvSensorW.getText().toString().trim());
                    item.setSensorH(mTvSensorH.getText().toString().trim());
                    item.setFocal_length(mTvFocalLength.getText().toString().trim());
                    dao.upDateCamera(currentCamera.getName(),item);
                    mCreateCamera.setVisibility(View.GONE);
                    mCustomCamera.setVisibility(View.VISIBLE);
                    getCustomCameraList();
                }
                break;
        }
    }

    private void showPopupWindow() {
        View convertView = LayoutInflater.from(this).inflate(R.layout.popup_map_type,null);
        LinearLayout llStandard = convertView.findViewById(R.id.ll_standard_map);
        llStandard.setOnClickListener(this);
        LinearLayout llSatellite = convertView.findViewById(R.id.ll_satellite_map);
        llSatellite.setOnClickListener(this);
        if (mPopupWindow == null) {
            mPopupWindow = new PopupWindow(convertView,RadioGroup.LayoutParams.WRAP_CONTENT,RadioGroup.LayoutParams.WRAP_CONTENT,true);
        }
        mPopupWindow.setTouchable(true);
        mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });

        mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup_grey_bg));
        mPopupWindow.showAsDropDown(mRlMapType,-200,20);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what){
            case 0:
                customFlag = EXIST_CAMRERA_SELECTED;
                mCreateCamera.setVisibility(View.VISIBLE);
                mCustomCamera.setVisibility(View.GONE);
                mSvAreaSetting.setVisibility(View.GONE);
                int position = msg.arg1;
                currentCamera = customCameras.get(position);
                mTvCameraName.setText(currentCamera.getName());
                mTvSensorW.setText(currentCamera.getSensorW()+"mm");
                mTvSensorH.setText(currentCamera.getSensorH()+"mm");
                mTvFocalLength.setText(currentCamera.getFocal_length()+"mm");
                break;
        }
        return false;
    }

}
